#include <iostream>
#include <chrono>
#include "file.h"
#include "krpc/sync.h"
#include "krpc/localstream.h"
#include "krpc/localdgram.h"
#include "krpc/func.h"

void test_stream(const std::string &content)
{
	KRPC::LOCALSTREAM com("unix");
	auto start = std::chrono::steady_clock::now();
	KRPC::SyncFunc<std::string(const std::string &)> func("echo", com, 3000);
	auto op = func(content);
	auto end = std::chrono::steady_clock::now();
	std::cout << "taste " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
	if (op && (*op) == content)
		std::cout << "echo succ" << std::endl;
	else
		std::cout << "echo fail" << std::endl;
}

void test_dgram(const std::string &content)
{
	KRPC::LOCALDGRAM com("unix");
	auto start = std::chrono::steady_clock::now();
	KRPC::SyncFunc<std::string(const std::string &)> func("echo", com, 3000);
	auto op = func(content);
	auto end = std::chrono::steady_clock::now();
	std::cout << "taste " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
	if (op && (*op == content)) {
		std::cout << "echo succ" << std::endl;
	} else
		std::cout << "echo fail" << std::endl;
}

int main(int argc, char **argv)
{
	if (argc < 2) {
		std::cerr << "please input a file path" << std::endl;
		return -1;
	}
	common::file f(argv[1], RDONLY);
	std::string content;
	f >> content;
	std::cout << "content size is " << content.size() << std::endl;	
	
	if (argc == 3) {
		test_dgram(content);
	} else {
		test_stream(content);
	}
	return 0;
}
